package com.yn.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.bind.v2.TODO;
import com.yn.common.exception.TransitException;
import com.yn.common.model.article.ArticleContent;

import com.yn.common.model.article.ArticleTitleManage;
import com.yn.common.model.user.User;
import com.yn.common.page.BasePage;
import com.yn.common.request.sc.ArticleTransitRequest;
import com.yn.common.request.sc.ArticleTransitSearchRequest;
import com.yn.common.request.sc.PageParamRequest;
import com.yn.common.vo.ArticleInfoVO;
import com.yn.common.vo.ArticleManageVO;
import com.yn.common.vo.ArticleTransitVO;
import com.yn.service.dao.ArticleContentDao;
import com.yn.service.service.IArticleContentService;
import com.yn.service.service.IArticleTitleManageService;
import com.yn.service.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * <p>
 * 文章内容(科研服务) 服务实现类
 * </p>
 *
 * @author xuquanjun
 * @since 2025-06-26
 */
@Service
@Slf4j
public class ArticleContentServiceImpl extends ServiceImpl<ArticleContentDao, ArticleContent> implements IArticleContentService {
    @Autowired
    private IArticleTitleManageService service;
    @Autowired
    private UserService userService;

    /**
     * 新增文章内容
     * @param request 新增文章参数
     * @return
     */
    @Override
    @Transactional
    public boolean create(ArticleTransitRequest request) {
        //根据titleManageId查询栏目名称,不存在返回非法参数
        ArticleTitleManage byId = service.getById(request.getTitleManageId());
        if(byId == null){
            throw new TransitException("栏目不存在");
        }
        ArticleContent one = lambdaQuery().eq(ArticleContent::getTitleManageId, request.getTitleManageId())
                .eq(ArticleContent::getArticleTitle, request.getArticleTitle()).one();
        if(one != null){
            throw new TransitException("文章已存在");
        }
        ArticleContent articleContent = new ArticleContent();
        articleContent.setTitleManageId(request.getTitleManageId());
        articleContent.setArticleTitle(request.getArticleTitle());
        articleContent.setArticleSource(request.getArticleSource());
        articleContent.setArticleContent(request.getArticleContent());

        //TODO 管理端用户名
        //String nickname= userService.getInfoException().getNickname();
        //articleContent.setCreateBy(nickname);
        //articleContent.setUpdateBy(nickname);
        return save(articleContent);
    }

    @Override
    @Transactional
    public boolean updateArticleContent(ArticleTransitRequest request) {

        log.info("修改文章内容,修改内容：{}", request);
        if (request.getId() == null) {
            return false;
        }
        ArticleContent content=new ArticleContent();
        BeanUtil.copyProperties(request,content);
        //获取用户id TODO 管理端用户
        //User user = userService.getInfoException();
        //content.setUpdateBy(user.getNickname());
        return updateById(content);
    }

    @Override
    public boolean deleteById(Long id) {
        log.info("删除文章内容管理,{}", id);
        return lambdaUpdate().eq(ArticleContent::getId, id).remove();
    }


    @Override
    public PageInfo<ArticleTransitVO> getAdminList(ArticleTransitSearchRequest request, PageParamRequest pageParamRequest) {
        Page<ArticleTitleManage> articlePage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        LambdaQueryWrapper<ArticleContent> lambdaQuery = Wrappers.lambdaQuery();
        if(request.getTitleManageId()!= null && request.getTitleManageId()>0){
            lambdaQuery.eq(ArticleContent::getTitleManageId, request.getTitleManageId());
        }

        if(request.getKeywords()!= null && !request.getKeywords().isEmpty()){
            lambdaQuery.and(i -> i.or().like(ArticleContent::getArticleTitle, request.getKeywords())
                    .or().like(ArticleContent::getArticleSource, request.getKeywords()));
        }

        List<ArticleContent> articleContentList = baseMapper.selectList(lambdaQuery);

        ArrayList<ArticleTransitVO> articleVoArrayList = new ArrayList<>();
        if(CollUtil.isEmpty(articleContentList)){
            return BasePage.copyPageInfo(articlePage, articleVoArrayList);
        }

        for (ArticleContent articleContent : articleContentList) {
            ArticleTransitVO articleVo = new ArticleTransitVO();
            BeanUtil.copyProperties(articleContent, articleVo);
            ArticleTitleManage manage = service.getById(articleContent.getTitleManageId());
            if (manage == null) {
               throw new TransitException( "文章栏目不存在");
            }
            articleVo.setTitleManageName(manage.getTitleName());
            articleVo.setTitleType(manage.getTitleType());
            //TODO 获取字典值
            /*Map<String, String> stringStringMap = service.showDictType2Map("");
            articleVo.setTitleTypeStr(stringStringMap.get(manage.getTitleType()));*/
            articleVoArrayList.add(articleVo);
        }

        return BasePage.copyPageInfo(articlePage, articleVoArrayList);

    }

    @Override
    public ArticleInfoVO getDetail(Long id) {
        ArticleContent articleContent = getById(id);
        if (ObjectUtil.isNull(articleContent)) {
            throw new TransitException("文章不存在");
        }
        ArticleInfoVO transform = ArticleInfoVO.transform(articleContent);
        ArticleTitleManage manage = service.getById(articleContent.getTitleManageId());
        if (manage == null) {
            throw new TransitException( "文章栏目不存在");
        }

        transform.setTitleManageName(manage.getTitleName());
        transform.setTitleType(manage.getTitleType());
        //TODO 获取字典值
        /*Map<String, String> stringStringMap = service.showDictType2Map("");
        transform.setTitleTypeStr(stringStringMap.get(manage.getTitleType()));*/
        return transform;
    }
}
